/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.maxato.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.table.TableColumn;

import nl.maxato.controller.Controller;
import nl.maxato.domain.Athlete;
import nl.maxato.domain.Wage;
import nl.maxato.modellen.AtleetTableModel;
import nl.maxato.modellen.WageTableModel;
import nl.maxato.util.ConfigurationFile;
import nl.maxato.util.ParameterNames;
import nl.maxato.xmlservices.ConvertXMLData;
import nl.maxato.xmlservices.XMLService;

/**
 *
 * @author Danny
 */
public class SalarisBerekenaarGUI extends javax.swing.JFrame {

    XMLService xmlService;
    ResourceBundle rb;
    Controller controller;
    List<Athlete> adf;
    String language;
    String country;
    Locale locale;
    ConfigurationFile cf;

    public SalarisBerekenaarGUI() {
        controller = new Controller();
        xmlService = XMLService.getInstance();
        //initComponents();
        makeComponents();

        cf = new ConfigurationFile();
        translateGUI(controller.getLanguague(cf.getLanguage()));
    }

    private void jMenuItem_ESTONIANActionPerformed(ActionEvent evt) {
        translateGUI(controller.getLanguague("et"));
    }

    private void jMenuItem_RUSSIANActionPerformed(ActionEvent evt) {
        translateGUI(controller.getLanguague("ru"));
    }

    private void jMenuItem_EngelsActionPerformed(java.awt.event.ActionEvent evt) {
        translateGUI(controller.getLanguague("en"));
    }

    private void jMenuItem_ITALIANActionPerformed(ActionEvent evt) {
        translateGUI(controller.getLanguague("it"));
    }

    private void jMenuItem_GREEKActionPerformed(ActionEvent evt) {
        translateGUI(controller.getLanguague("el"));
    }

    private void jMenuItem_CZECHActionPerformed(ActionEvent evt) {
        translateGUI(controller.getLanguague("cs"));
    }

    private void jMenuItem_NederlandsActionPerformed(java.awt.event.ActionEvent evt) {
        translateGUI(controller.getLanguague("nl"));
    }

    private void jMenuItem_PolishActionPerformed(java.awt.event.ActionEvent evt) {
        translateGUI(controller.getLanguague("pl"));
    }

    private void jMenuItem_DEUTSCHActionPerformed(ActionEvent evt) {
        translateGUI(controller.getLanguague("de"));
    }

    private void jMenuItem_FRANCAISActionPerformed(ActionEvent evt) {
        translateGUI(controller.getLanguague("fr"));
    }

    private void jMenuItem_ESPANOLActionPerformed(ActionEvent evt) {
        translateGUI(controller.getLanguague("es"));
    }

    private void jMenuItem_PORTUGUESEActionPerformed(ActionEvent evt) {
        translateGUI(controller.getLanguague("pt"));
    }
    
    private void jMenuItem_HUNGARIANActionPerformed(ActionEvent evt) {
        translateGUI(controller.getLanguague("hu"));
    }
    
    private void jMenuItem_AfsluitenActionPerformed(java.awt.event.ActionEvent evt) {
        this.dispose();
    }

    private void jButton_Calculate_WageActionPerformed(ActionEvent evt) {
        Double maxid = 0.0;
        Double fans = 0.0;
        Double constant = 0.0;
        try {
            maxid = Double.parseDouble(jTextField_Maxid.getText());
            fans = Double.parseDouble(jTextField_Fans.getText());
            constant = Double.parseDouble(jTextField_Constant.getText());
            List<Athlete> athletes = new ArrayList<Athlete>();
            athletes = xmlService.xmlToAthletes();
            List<Wage> wages = controller.calculateWageForNextSeason(maxid, fans, constant, athletes, rb);
            int totalWage = 0;
            for (int i = 0; i < wages.size(); i++) {
                totalWage += wages.get(i).getNewSal();
            }

            WageTableModel wtm = new WageTableModel(wages);
            wtm.setColumnNames(rb.getString("NAME"), rb.getString("AGE"), rb.getString("MAXID"), rb.getString("FANS"), rb.getString("FOREIGNER"), rb.getString("WAGE"), rb.getString("NEW_WAGE"), rb.getString("DIFFERENCE"));
            jTable_Wage.setModel(wtm);
            TableColumn column;
            jTable_Wage.getColumnModel().getColumn(0).setPreferredWidth(165);
            for (int i = 1; i < 8; i++) {
                column = jTable_Wage.getColumnModel().getColumn(i);
                column.setPreferredWidth(115);
            }
            jTable_Wage.setAutoCreateRowSorter(true);
            jTable_Wage.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            jLabel_Total_Wage_Now_Value.setText(Integer.toString(controller.getTotalStatistics(athletes, "wage")));
            jLabel_New_Total_Wage_Value.setText("" + totalWage);
            jLabel_Difference_Wage_Value.setText(Integer.toString(totalWage - controller.getTotalStatistics(athletes, "wage")));

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, rb.getString("ERROR_NOT_A_NUMBER"), rb.getString("ERROR_SCREEN"), 2);
        }
    }

    private void jButton_ExplanationActionPerformed(ActionEvent evt) {
        JOptionPane.showMessageDialog(null, "<html>" + rb.getString("EXPLANATION_WAGE") + "</html>", rb.getString("MAXATO_WAGE_INFORMATION"), JOptionPane.INFORMATION_MESSAGE);
    }

    private void translateGUI(ResourceBundle b) {
        this.rb = b;
        this.setTitle(b.getString("MAXATO_CALCULATE_WAGE"));
        jMenu_File.setText(b.getString("FILE"));
        jMenu_Language.setText(b.getString("LANGUAGE"));
        jMenuItem_Close.setText(b.getString("CLOSE"));
        jMenuItem_English.setText(ParameterNames.languageENGLISH);
        jMenuItem_Dutch.setText(ParameterNames.languageDUTCH);
        jMenuItem_Polish.setText(ParameterNames.languagePOLISH);
        jMenuItem_Deutsch.setText(ParameterNames.languageDEUTSCH);
        jMenuItem_Francais.setText(ParameterNames.languageFRANCAIS);
        jMenuItem_Espanol.setText(ParameterNames.languageESPANOL);
        jMenuItem_Portuguese.setText(ParameterNames.languagePORTUGUESE);
        jMenuItem_Czech.setText(ParameterNames.languageCZECH);
        jMenuItem_Greek.setText(ParameterNames.languageGREEK);
        jMenuItem_Italian.setText(ParameterNames.languageITALIAN);
        jMenuItem_Hungarian.setText(ParameterNames.languageHungarian);
        jMenuItem_Estonian.setText(ParameterNames.languageESTONIAN);
        jMenuItem_Russian.setText(ParameterNames.languageRUSSIAN);

//        jLabel_Explanation.setText("<html>" + rb.getString("EXPLANATION_WAGE_CALCULATOR") + "</html>");
        jLabel_Total_Wage_Now.setText(b.getString("TOTAL_WAGE") + ":");
        jButton_Calculate_Wage.setText(b.getString("CALCULATE"));
        jLabel_New_Total_Wage.setText(b.getString("TOTAL_NEW_WAGE") + ":");
        jLabel_Difference.setText(b.getString("DIFFERENCE") + ":");
        jLabel_Total_Wage_Now_Value.setText("0");
        jLabel_Difference_Wage_Value.setText("0");
        jLabel_New_Total_Wage_Value.setText("0");
        jButton_Explanation.setText(b.getString("EXPLANATION_WAGE_CALCULATION"));

        jLabel_Maxid.setText(b.getString("NEW_WAGE") + "= (" + b.getString("MAXID") + " *");
        jLabel_Fans.setText(")+(" + b.getString("FANS") + " *");
        jLabel_Min.setText(") -");
        jLabel_Constant.setText(" (" + b.getString("CONSTANT") + ")");
        jButton_Calculate_WageActionPerformed(null);
    }

    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
        //  java.net.URL imgURL = path;
        if (path != null) {
            return new ImageIcon(path);
        } else {
            System.err.println("Couldn't find image file: " + path);
            return null;
        }
    }
    //Datavelden makeComponents
    private JMenuBar jMenuBar1;
    private JMenu jMenu_File;
    private JMenu jMenu_Language;
    private JMenuItem jMenuItem_Close;
    private javax.swing.JMenuItem jMenuItem_English;
    private javax.swing.JMenuItem jMenuItem_Dutch;
    private javax.swing.JMenuItem jMenuItem_Polish;
    private javax.swing.JMenuItem jMenuItem_Deutsch;
    private javax.swing.JMenuItem jMenuItem_Espanol;
    private javax.swing.JMenuItem jMenuItem_Francais;
    private javax.swing.JMenuItem jMenuItem_Portuguese;
    private javax.swing.JMenuItem jMenuItem_Estonian;
    private javax.swing.JMenuItem jMenuItem_Russian;
    private javax.swing.JMenuItem jMenuItem_Italian;
    private javax.swing.JMenuItem jMenuItem_Czech;
    private javax.swing.JMenuItem jMenuItem_Greek;
    private javax.swing.JMenuItem jMenuItem_Hungarian;
    private javax.swing.JButton jButton_Calculate_Wage;
    private javax.swing.JButton jButton_Explanation;
    private javax.swing.JComboBox jComboBox_Specialty;
    private javax.swing.JComboBox jComboBox_Discipline;
    private javax.swing.JComboBox jComboBox_Sex;
    private javax.swing.JLabel jLabel_Explanation;
    private javax.swing.JLabel jLabel_Total_Wage_Now;
    private javax.swing.JLabel jLabel_New_Total_Wage;
    private javax.swing.JLabel jLabel_Total_Wage_Now_Value;
    private javax.swing.JLabel jLabel_New_Total_Wage_Value;
    private javax.swing.JLabel jLabel_Difference;
    private javax.swing.JLabel jLabel_Difference_Wage_Value;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable_Wage;
    JLabel jLabel_Maxid;
    JTextField jTextField_Maxid;
    JLabel jLabel_Fans;
    JTextField jTextField_Fans;
    JLabel jLabel_Min;
    JTextField jTextField_Constant;
    JLabel jLabel_Constant;

    private void makeComponents() {
        //Creating the Frame
        setResizable(false);
        this.setDefaultCloseOperation(SalarisBerekenaarGUI.DISPOSE_ON_CLOSE);
        //this.setSize(800, 500);
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width - 1005) / 2, (screenSize.height - 505) / 2, 1005, 505);

        //Creating the MenuBar and adding components
        jMenuBar1 = new JMenuBar();
        jMenu_File = new JMenu();
        jMenu_Language = new JMenu();
        jMenuBar1.add(jMenu_File);
        jMenuBar1.add(jMenu_Language);
        ImageIcon icon;
        icon = createImageIcon("img/close.png");
        jMenuItem_Close = new JMenuItem(icon);
        jMenuItem_Close.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem_AfsluitenActionPerformed(evt);
            }
        });
        icon = createImageIcon("img/gb.png");
        jMenuItem_English = new JMenuItem(icon);
        jMenuItem_English.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem_EngelsActionPerformed(evt);
            }
        });

        icon = createImageIcon("img/et.png");
        jMenuItem_Estonian = new JMenuItem(icon);
        jMenuItem_Estonian.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem_ESTONIANActionPerformed(evt);
            }
        });
        icon = createImageIcon("img/ru.png");
        jMenuItem_Russian = new JMenuItem(icon);
        jMenuItem_Russian.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem_RUSSIANActionPerformed(evt);
            }
        });
        icon = createImageIcon("img/nl.png");
        jMenuItem_Dutch = new JMenuItem(icon);
        jMenuItem_Dutch.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem_NederlandsActionPerformed(evt);
            }
        });
        icon = createImageIcon("img/pl.png");
        jMenuItem_Polish = new JMenuItem(icon);
        jMenuItem_Polish.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem_PolishActionPerformed(evt);
            }
        });
        icon = createImageIcon("img/pt.png");
        jMenuItem_Portuguese = new JMenuItem(icon);
        jMenuItem_Portuguese.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem_PORTUGUESEActionPerformed(evt);
            }
        });
        icon = createImageIcon("img/de.png");
        jMenuItem_Deutsch = new JMenuItem(icon);
        jMenuItem_Deutsch.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem_DEUTSCHActionPerformed(evt);
            }
        });
        icon = createImageIcon("img/fr.png");
        jMenuItem_Francais = new JMenuItem(icon);
        jMenuItem_Francais.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem_FRANCAISActionPerformed(evt);
            }
        });
        icon = createImageIcon("img/es.png");
        jMenuItem_Espanol = new JMenuItem(icon);
        jMenuItem_Espanol.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem_ESPANOLActionPerformed(evt);
            }
        });
        icon = createImageIcon("img/gr.png");
        jMenuItem_Greek = new JMenuItem(icon);
        jMenuItem_Greek.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem_GREEKActionPerformed(evt);
            }
        });
        icon = createImageIcon("img/cz.png");
        jMenuItem_Czech = new JMenuItem(icon);
        jMenuItem_Czech.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem_CZECHActionPerformed(evt);
            }
        });
        icon = createImageIcon("img/hu.png");
        jMenuItem_Hungarian = new JMenuItem(icon);
        jMenuItem_Hungarian.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem_HUNGARIANActionPerformed(evt);
            }
        });
        icon = createImageIcon("img/it.png");
        jMenuItem_Italian = new JMenuItem(icon);
        jMenuItem_Italian.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem_ITALIANActionPerformed(evt);
            }
        });
        jMenu_File.add(jMenuItem_Close);
        jMenu_Language.add(jMenuItem_Czech);
        jMenu_Language.add(jMenuItem_Deutsch);
        jMenu_Language.add(jMenuItem_Estonian);
        jMenu_Language.add(jMenuItem_English);
        jMenu_Language.add(jMenuItem_Greek);
        jMenu_Language.add(jMenuItem_Espanol);
        jMenu_Language.add(jMenuItem_Francais);
        jMenu_Language.add(jMenuItem_Hungarian);
        jMenu_Language.add(jMenuItem_Italian);
        jMenu_Language.add(jMenuItem_Dutch);
        jMenu_Language.add(jMenuItem_Polish);
        jMenu_Language.add(jMenuItem_Portuguese);
        jMenu_Language.add(jMenuItem_Russian);

//        JPanel panelInformation = new JPanel();
//        jLabel_Explanation = new JLabel();
//        panelInformation.add(jLabel_Explanation);

        JPanel panelSum = new JPanel();
        jLabel_Maxid = new JLabel("Nieuw Salaris= (MAXID *");
        jTextField_Maxid = new JTextField("0.3", 5);

        jLabel_Fans = new JLabel(")+(Fans *");
        jTextField_Fans = new JTextField("31.23", 5);
        jLabel_Min = new JLabel(") -");
        jTextField_Constant = new JTextField("34.59", 5);
        jLabel_Constant = new JLabel(" (constante)");
        jButton_Explanation = new JButton();
        jButton_Explanation.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_ExplanationActionPerformed(evt);
            }
        });
        panelSum.add(jLabel_Maxid);
        panelSum.add(jTextField_Maxid);
        panelSum.add(jLabel_Fans);
        panelSum.add(jTextField_Fans);
        panelSum.add(jLabel_Min);
        panelSum.add(jTextField_Constant);
        panelSum.add(jLabel_Constant);
        panelSum.add(jButton_Explanation);

        JPanel panels = new JPanel();

        jLabel_Total_Wage_Now = new JLabel();
        jLabel_Total_Wage_Now_Value = new JLabel();
        jLabel_New_Total_Wage = new JLabel();
        jLabel_New_Total_Wage_Value = new JLabel();
        jLabel_Difference = new JLabel();
        jLabel_Difference_Wage_Value = new JLabel();
        jButton_Calculate_Wage = new JButton();
        jButton_Calculate_Wage.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_Calculate_WageActionPerformed(evt);
            }
        });
        panels.add(jLabel_Total_Wage_Now);// Components Added using Flow Layout
        panels.add(jLabel_Total_Wage_Now_Value);
        panels.add(jLabel_New_Total_Wage);
        panels.add(jLabel_New_Total_Wage_Value);
        panels.add(jLabel_Difference);
        panels.add(jLabel_Difference_Wage_Value);
        panels.add(jButton_Calculate_Wage);

        jLabel_Difference_Wage_Value.setText("0");
        jLabel_New_Total_Wage_Value.setText("0");
        jLabel_Total_Wage_Now_Value.setText("0");
        //Panel for Center Center
        jTable_Wage = new JTable();
        jTable_Wage.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{},
                new String[]{}));

        jScrollPane1 = new JScrollPane();
        jScrollPane1.setPreferredSize(new Dimension(183, 395));
        jScrollPane1.setViewportView(jTable_Wage);

        //Create Center Panel
        JPanel p1 = new JPanel();
        p1.setLayout(new BorderLayout());
        //p1.add(panelInformation, BorderLayout.NORTH);
        p1.add(panelSum, BorderLayout.NORTH);
        p1.add(panels, BorderLayout.CENTER);
        // p1.add(new JButton("B"), BorderLayout.NORTH);
        // p1.add(jScrollPane1, BorderLayout.SOUTH);




        //Add panels to GUI
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(BorderLayout.NORTH, jMenuBar1);
        this.getContentPane().add(BorderLayout.CENTER, p1);
        this.getContentPane().add(BorderLayout.SOUTH, jScrollPane1);
        // this.getContentPane().add(BorderLayout.CENTER, p1);
        this.setVisible(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(final String args[]) {
        //zet de GUI om in Windows-look

        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception e) {
            System.out.println("Unable to load Windows look and feel");
        }
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                new SalarisBerekenaarGUI().setVisible(true);
            }
        });
    }
}
